package socketTest.chapter02;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;

/**
 * 名称: ToUpperTCPServer.java <br>
 * 描述: <br>
 * 最近修改时间:2018/5/3 17:08 <br>
 *
 * @version [版本号, V1.0]
 * @since 2018/5/3 17:08
 * @author zhipingzhang  
 */
public class ToUpperTCPServer {

    //服务器IP
    public static final String SERVER_IP = "127.0.0.1";

    //服务器端口号
    public static final int SERVER_PORT = 10005;

    //最多处理1024个字符
    public static final int MAX_BYTES = 1024;
    //请求终结字符串


    /***
     * 启动服务器
     * @param serverIp 服务器监听的端口号，服务器ip无需指定，系统自动分配
     * @param serverPort 服务器监听的端口号，服务器ip无需指定，系统自动分配
     */
    public void startServer(String serverIp, int serverPort) {
        try {
            //创建服务器地址对象
            InetAddress serverAddr;
            try {
                serverAddr = InetAddress.getByName(serverIp);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
                return;
            }
            try(ServerSocket serverSocket = new ServerSocket(serverPort,5,serverAddr)){
                System.out.println("TCP-server启动..............");
                while (true){
                    StringBuilder recvStrBuilder = new StringBuilder();

                    //有客户端向服务器发起tcp连接时，accept会返回一个Socket
                    //该Socket的対端就是客户端的Socket
                    //具体过程可以查看TCP三次握手过程
                    System.out.println("等待获取连接请求..............");
                    try (Socket connection = serverSocket.accept()) {
                        InputStream in = connection.getInputStream();
                        System.out.println("收到连接请求..............");
                        InetAddress client = connection.getInetAddress();
                        System.out.println("客户端地址："+client.getHostAddress());
                        //读取客户端的请求字符串，请求字符串以#终结
                        for (int c = in.read(); c > 0; c = in.read()) {
                            recvStrBuilder.append((char)c);
                        }
                        System.out.println("客户端请求数据："+recvStrBuilder.toString());

                        String recvStr = recvStrBuilder.toString();

                        //向客户端写出处理后的字符串
                        OutputStream out = connection.getOutputStream();
                        out.write(recvStr.toUpperCase().getBytes());
                        out.flush();
                        connection.shutdownOutput();
                        System.out.println("本次请求结束..............");
                        System.out.println("-------------------------");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e2) {
            e2.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ToUpperTCPServer server = new ToUpperTCPServer();
        server.startServer(SERVER_IP, SERVER_PORT);
    }
}